package math.geom2d;

import math.base.CCS2d;
import math.base.Point2d;
import math.base.Vector2d;
import math.geomUtils.CurveType;

/**
 * @author Saga
 *
 */
public class Ellipse2d extends ConicalCurve2d {

    public Ellipse2d(final CCS2d _pos, final double _majorRadius, final double _minorRadius) {
        if (this.majorRadius <= this.minorRadius || this.minorRadius < 0.0) {
            throw new ArithmeticException("");
        }

        this.pos = _pos;
        this.majorRadius = _majorRadius;
        this.minorRadius = _minorRadius;
    }

    @Override
    public final CurveType getCurveType() {
        return CurveType.ELLIPSE;
    }

    @Override
    public final void reverse() {
        this.pos.yReversed();
    }

    @Override
    public final Curve2d reversed() {
        return new Ellipse2d(this.pos.yReversed(), majorRadius, minorRadius);
    }

    @Override
    public final boolean isPeriodic() {
        return true;
    }

    @Override
    public final double getPeriod() {
        return Math.PI * 2.0;
    }

    @Override
    public final double getFirstParameter() {
        return 0.0;
    }

    @Override
    public final double getLastParameter() {
        return Math.PI * 2.0;
    }

    public final void setMajorRadius(final double _majorRadius) {
        if (_majorRadius <= this.minorRadius) {
            throw new ArithmeticException("");
        }

        this.majorRadius = _majorRadius;
    }

    public final double getMajorRadius() {
        return this.majorRadius;
    }

    public final void setMinorRadius(final double _minorRadius) {
        if (_minorRadius >= this.majorRadius || _minorRadius < 0.0) {
            throw new ArithmeticException("");
        }

        this.minorRadius = _minorRadius;
    }

    public final double getMinorRadius() {
        return this.minorRadius;
    }

    @Override
    public final Point2d calcValue(final double _param) {
        final double cosVal = Math.cos(_param);
        final double sinVal = Math.sin(_param);

        return this.pos.getOrigin().added(
                this.pos.getDirectX().multiplied(cosVal * this.majorRadius).added(this.pos
                        .getDirectY().multiplied(sinVal * this.minorRadius)));
    }

    @Override
    public final Vector2d calcDerivative(final double _param) {
        final double cosVal = Math.cos(_param);
        final double sinVal = Math.sin(_param);

        return this.pos.getDirectX().multiplied(-sinVal * this.majorRadius).added(
                this.pos.getDirectY().multiplied(cosVal * this.minorRadius));
    }

    @Override
    public final Vector2d calcDerivativeN(final double _param, final int _order) {
        if (_order < 1) {
            throw new ArithmeticException("");
        }

        final double cosVal = Math.cos(_param);
        final double sinVal = Math.sin(_param);

        switch (_order % 4) {
        case 0:
            return this.pos.getDirectX().multiplied(cosVal * this.majorRadius).added(
                    this.pos.getDirectY().multiplied(sinVal * this.minorRadius));

        case 1:
            return this.pos.getDirectX().multiplied(-sinVal * this.majorRadius).added(
                    this.pos.getDirectY().multiplied(cosVal * this.minorRadius));

        case 2:
            return this.pos.getDirectX().multiplied(-cosVal * this.majorRadius).added(
                    this.pos.getDirectY().multiplied(-sinVal * this.minorRadius));

        case 3:
            return this.pos.getDirectX().multiplied(sinVal * this.majorRadius).added(
                    this.pos.getDirectY().multiplied(-cosVal * this.minorRadius));
        }

        throw new ArithmeticException("");
    }

    @Override
    public final void calcValueAndDerivative1(final double _param, final Point2d _pt,
            final Vector2d _der1) {
        final double cosVal = Math.cos(_param);
        final double sinVal = Math.sin(_param);

        _pt.setCoord(this.pos.getOrigin().getCoord()
                .added(this.pos.getDirectX().getCoord().multiplied(cosVal * this.majorRadius)
                        .added(this.pos.getDirectY().getCoord()
                                .multiplied(sinVal * this.minorRadius))));
        _der1.setCoord(this.pos.getDirectX().getCoord().multiplied(-sinVal * this.majorRadius)
                .added(this.pos.getDirectY().getCoord().multiplied(cosVal * this.minorRadius)));
    }

    @Override
    public final void calcValueAndDerivative2(final double _param, final Point2d _pt,
            final Vector2d _der1, final Vector2d _der2) {
        final double cosVal = Math.cos(_param);
        final double sinVal = Math.sin(_param);

        _pt.setCoord(this.pos.getOrigin().getCoord()
                .added(this.pos.getDirectX().getCoord().multiplied(cosVal * this.majorRadius)
                        .added(this.pos.getDirectY().getCoord()
                                .multiplied(sinVal * this.minorRadius))));
        _der1.setCoord(this.pos.getDirectX().getCoord().multiplied(-sinVal * this.majorRadius)
                .added(this.pos.getDirectY().getCoord().multiplied(cosVal * this.minorRadius)));
        _der2.setCoord(this.pos.getDirectX().getCoord().multiplied(-cosVal * this.majorRadius)
                .added(this.pos.getDirectY().getCoord().multiplied(-sinVal * this.minorRadius)));
    }

    @Override
    public final void calcValueAndDerivative3(final double _param, final Point2d _pt,
            final Vector2d _der1, final Vector2d _der2, final Vector2d _der3) {
        final double cosVal = Math.cos(_param);
        final double sinVal = Math.sin(_param);

        _pt.setCoord(this.pos.getOrigin().getCoord()
                .added(this.pos.getDirectX().getCoord().multiplied(cosVal * this.majorRadius)
                        .added(this.pos.getDirectY().getCoord()
                                .multiplied(sinVal * this.minorRadius))));
        _der1.setCoord(this.pos.getDirectX().getCoord().multiplied(-sinVal * this.majorRadius)
                .added(this.pos.getDirectY().getCoord().multiplied(cosVal * this.minorRadius)));
        _der2.setCoord(this.pos.getDirectX().getCoord().multiplied(-cosVal * this.majorRadius)
                .added(this.pos.getDirectY().getCoord().multiplied(-sinVal * this.minorRadius)));
        _der3.setCoord(this.pos.getDirectX().getCoord().multiplied(sinVal * this.majorRadius)
                .added(this.pos.getDirectY().getCoord().multiplied(-cosVal * this.minorRadius)));
    }

    @Override
    public final Ellipse2d clone() {
        return new Ellipse2d(this.pos.clone(), this.majorRadius, this.minorRadius);
    }

    private double majorRadius;
    private double minorRadius;
}
